<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Content;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Content\Ctl\ContentCtl;
use Srv\Apps\Proto\DTO\Content\ContentAddDTO;
use Srv\Apps\Proto\DTO\Content\ContentEditDTO;
use Srv\Apps\Proto\DTO\Content\ContentListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Content\ContentListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class ContentAct extends ActionAbstract
{
    /**
     * @url /content/list
     * @auth getContentList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Content.ContentListDTO
     * @rsp Srv.Apps.Proto.VO.Content.ContentListVO
     * 内容列表
     */
    public function getContentList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof ContentListDTO) || !($VOClass instanceof ContentListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = ContentCtl::tag()->getContentList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /content/add
     * @auth contentAdd
     * @req Srv.Apps.Proto.DTO.Content.ContentAddDTO
     * 内容新增
     */
    public function contentAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof ContentAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $skcyId     = ContentCtl::tag()->contentAdd($setData);
        if($skcyId < 1){
            $errMsg = '内容新增失败';
            $LogCtl->logAdd(LogCtl::CONTENT_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '内容新增成功';
        $LogCtl->logAdd(LogCtl::CONTENT_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /content/edit
     * @auth contentEdit
     * @req Srv.Apps.Proto.DTO.Content.ContentEditDTO
     * 内容编辑
     */
    public function contentEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof ContentEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $cttrId             = intval($DTOClass->getCttrId());
        $setData            = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl             = LogCtl::tag();
        $ContentCtl         = ContentCtl::tag();
        $infoSrc            = $ContentCtl->getContentSrc($cttrId);
        if(!isset($infoSrc['cttr_id'])){
            $logData        = $setData;
            $errMsg         = '内容不存在';
            $LogCtl->logAdd(LogCtl::CONTENT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result             = $ContentCtl->contentEdit($cttrId, $setData);
        $logData            = ['new' => $setData, 'old' => $infoSrc];
        if(!$result){
            $errMsg         = '内容编辑失败';
            $LogCtl->logAdd(LogCtl::CONTENT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg             = '内容编辑成功';
        $LogCtl->logAdd(LogCtl::CONTENT_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}